package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.storage.ServerCnInfoStorage;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerCnInfoTable implements ServerCnInfoStorage {
  private static final Log _log = LogFactory.getLog(ServerCnInfoTable.class);
  
  public void create(L1PcInstance pc, L1Item itemtmp, long count, boolean mode, int itemid_cn) {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      Timestamp lastactive = new Timestamp(System.currentTimeMillis());
      con = DatabaseFactory.get().getConnection();
      String sqlstr = "INSERT INTO `character_角色_商城` SET `buypc`=?,`itemname`=?,`itemid`=?,`payvalue`=?,`selling_price`=?,`time`=?,`pcobjid`=?,`mode`=?";
      pstm = con.prepareStatement("INSERT INTO `character_角色_商城` SET `buypc`=?,`itemname`=?,`itemid`=?,`payvalue`=?,`selling_price`=?,`time`=?,`pcobjid`=?,`mode`=?");
      String pcinfo = "(玩家)";
      if (pc.isGm())
        pcinfo = "(管理者)"; 
      int i = 0;
      L1Item temp = ItemTable.get().getTemplate(itemid_cn);
      pstm.setString(++i, String.valueOf(String.valueOf(pc.getName()) + pcinfo));
      pstm.setString(++i, String.valueOf(itemtmp.getName()));
      pstm.setInt(++i, itemtmp.getItemId());
      pstm.setInt(++i, temp.getItemId());
      pstm.setLong(++i, count);
      pstm.setTimestamp(++i, lastactive);
      pstm.setInt(++i, pc.getId());
      pstm.setBoolean(++i, mode);
      pstm.execute();
      if (mode) {
        _log
          .info("建立商城纪录 人物:" + pc.getName() + " 买入:" + itemtmp.getName() + " 花费商城币:" + count);
      } else {
        _log
          .info("建立商城纪录 人物:" + pc.getName() + " 卖出:" + itemtmp.getName() + " 获得商城币:" + count);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
}
